package com.baba.bracelet.controller;

import com.alibaba.fastjson.JSONObject;
import com.baba.bracelet.base.BaseApiService;
import com.baba.bracelet.base.BaseResponse;
import com.baba.bracelet.base.BaseWebController;
import com.baba.bracelet.config.ConfigPath;
import com.baba.bracelet.constants.Constants;
import com.baba.bracelet.service.TokenService;
import com.baba.bracelet.utils.OkHttpUtil;
import com.baba.bracelet.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wulongbo
 * @version 1.0
 * @date 2020/6/5 11:34
 * @description
 */

@RestController
@RequestMapping("/device")
@Api(tags = "设备控制管理模块")
public class DeviceController extends BaseApiService<JSONObject> {

    @Autowired
    private ConfigPath configPath;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取第三方token
     * @return
     */
    private String getTokenOk(){
        //获取token
        String token= tokenService.getToken(configPath.getTokenURL(),configPath.getCustomerId(),configPath.getSecretKey());
        System.out.println("token:"+token);
        return token;
    }

    private String getToken(String deviceId){
        String token=redisUtil.getString(deviceId);
        if(StringUtils.isEmpty(token)){
            token=this.getTokenOk();
            //生成对应用户令牌存放在redis中  设备token有效期180分钟
            redisUtil.setString(deviceId,token, Constants.IMEI_TOKEN);
        }
        return token;
    }

    @ApiOperation(value="恢复出⼚设置", notes="恢复出⼚设置")
    @ApiImplicitParam(dataType = "string",name = "deviceId",value = "设备IMEI号",required = true)
    @RequestMapping("/reset")
    public BaseResponse<JSONObject> restoreSetting(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getRestoreURL(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("恢复出⼚设置失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="重启", notes="重启")
    @ApiImplicitParam(dataType = "string",name = "deviceId",value = "设备IMEI号",required = true)
    @RequestMapping("/restart")
    public BaseResponse<JSONObject> restart(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getRestart(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("重启失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="关机", notes="关机")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "time", dataType = "int", required = true, value = "延迟关机时间，0是立即关机。（秒）") })
    @RequestMapping("/shutdown")
    public BaseResponse<JSONObject> shutdown(String deviceId,int time) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("time",time);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getShutdown(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("关机失败，请联系后台管理员Bob");
    }


    @ApiOperation(value="连接WIFI", notes="连接WIFI")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "WIFI名称（打开时必填）"),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "String", required = true, value = "WIFI密码（打开时必填）"),
            @ApiImplicitParam(paramType = "query", name = "open", dataType = "boolean", required = true, value = "开关（true/false）") })
    @RequestMapping("/linkWIFI")
    public BaseResponse<JSONObject> linkWIFI(String deviceId,String name,String password,boolean open) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("name",name);
        map.put("password",password);
        map.put("open",open);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getLinkWIFI(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("连接WIFI失败，请联系后台管理员Bob");
    }


    @ApiOperation(value=" 提⽰效果", notes=" 提⽰效果")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "systemVolume", dataType = "int", required = true, value = "系统⾳量(0 静⾳；1 低；2 中；3⾼)"),
            @ApiImplicitParam(paramType = "query", name = "ringVolume", dataType = "int", required = true, value = "铃声⾳量(0 静⾳；1 低；2 中；3⾼)"),
            @ApiImplicitParam(paramType = "query", name = "clockVolume", dataType = "int", required = true, value = "闹钟⾳量(0 静⾳；1 低；2 中；3⾼)"),
            @ApiImplicitParam(paramType = "query", name = "light", dataType = "boolean", required = true, value = "是否开启灯效（true/false）")})
    @RequestMapping("/notification")
    public BaseResponse<JSONObject> notification(String deviceId,int systemVolume,int ringVolume ,int clockVolume,boolean light) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("systemVolume",systemVolume);
        map.put("ringVolume",ringVolume);
        map.put("clockVolume",clockVolume);
        map.put("light",light);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getNotification(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("提⽰效果出错，请联系后台管理员Bob");
    }

    @ApiOperation(value="通话⽩名单", notes="通话⽩名单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "open", dataType = "boolean", required = true, value = "是否开启（true/false）")})
    @RequestMapping("/callWhitelist")
    public BaseResponse<JSONObject> callWhitelist(String deviceId,boolean open) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("open",open);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getCallWhitelist(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("通话⽩名单调取失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="允许外拨", notes="允许外拨")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "open", dataType = "boolean", required = true, value = "是否开启（true/false）")})
    @RequestMapping("/outgoingCall")
    public BaseResponse<JSONObject> outgoingCall(String deviceId,boolean open) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("open",open);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getOutgoingCall(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("允许外拨调取失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="查找设备", notes="查找设备")
    @ApiImplicitParam(dataType = "string",name = "deviceId",value = "设备IMEI号",required = true)
    @RequestMapping("/find")
    public BaseResponse<JSONObject> find(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getFind(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("查找设备失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="添加或编辑联系⼈", notes="添加或编辑联系⼈")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "联系⼈ID"),
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "名称"),
            @ApiImplicitParam(paramType = "query", name = "number", dataType = "String", required = true, value = "号码")})
    @RequestMapping("/addOrEditContact")
    public BaseResponse<JSONObject> addOrEditContact(String deviceId,int id,String name ,String number) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("id",id);
        map.put("name",name);
        map.put("number",number);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getAddOrEditContact(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("添加或编辑联系⼈失败，请联系后台管理员Bob");
    }


    @ApiOperation(value="删除联系⼈", notes="删除联系⼈")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "联系⼈ID")})
    @RequestMapping("/delContact")
    public BaseResponse<JSONObject> delContact(String deviceId,int id) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("id",id);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getDelContact(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("删除联系⼈出错，请联系后台管理员Bob");
    }

    @ApiOperation(value="添加或编辑闹钟⼈", notes="添加或编辑闹钟⼈")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "联系⼈ID"),
            @ApiImplicitParam(paramType = "query", name = "open", dataType = "boolean", required = true, value = "是否开启（true/false）"),
            @ApiImplicitParam(paramType = "query", name = "time", dataType = "String", required = true, value = "时间格式HH:mm"),
            @ApiImplicitParam(paramType = "query", name = "repeat", dataType = "String", required = true, value = "重复（0000000 只响⼀次；1111111 每天； 0000000 对应星期⼀到星期⽇）"),
            @ApiImplicitParam(paramType = "query", name = "remark", dataType = "String", required = false, value = "备注")})
    @RequestMapping("/addOrEditAlarm")
    public BaseResponse<JSONObject> addOrEditAlarm(String deviceId,int id,boolean open,String time,String repeat ,String remark) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("id",id);
        map.put("open",open);
        map.put("time",time);
        map.put("repeat",repeat);
        map.put("remark",remark);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getAddOrEditAlarm(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError(" 添加或编辑闹钟失败，请联系后台管理员Bob");
    }

    @ApiOperation(value="静默监听", notes="静默监听")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "String", required = true, value = "⼿机号码")})
    @RequestMapping("/monitor")
    public BaseResponse<JSONObject> monitor(String deviceId,String phone) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("phone",phone);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getMonitor(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("静默监听出错，请联系后台管理员Bob");
    }


    @ApiOperation(value="立即上传位置", notes="立即上传位置")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号")})
    @RequestMapping("/upLocation")
    public BaseResponse<JSONObject> upLocation(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getUpLocation(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("立即上传位置出错，请联系后台管理员Bob");
    }


    @ApiOperation(value="设置模式", notes="设置模式")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号"),
            @ApiImplicitParam(paramType = "query", name = "model", dataType = "int", required = true, value = "模式（0 离线模式; 1- 省电模式； 2- 普通模式（默认）")})
    @RequestMapping("/model")
    public BaseResponse<JSONObject> model(String deviceId,int model) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        map.put("model",model);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getModel(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("设置模式出错，请联系后台管理员Bob");
    }

    @ApiOperation(value="上传及时信息", notes="上传及时信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号")})
    @RequestMapping("/upInfo")
    public BaseResponse<JSONObject> upInfo(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doPostHttpRequest2(configPath.getUpInfo(),map);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("上传及时信息出错，请联系后台管理员Bob");
    }

    @ApiOperation(value="获取所有联系⼈", notes="获取所有联系⼈")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号")})
    @RequestMapping("/getContacts")
    public BaseResponse<JSONObject> getContacts(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doGetHttpRequest(configPath.getContacts(),map,null);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("获取所有联系⼈出错，请联系后台管理员Bob");
    }

    @ApiOperation(value="获取所有闹钟", notes="获取所有闹钟")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备IMEI号")})
    @RequestMapping("/getAlarms")
    public BaseResponse<JSONObject> getAlarms(String deviceId) throws IOException {
        String token=this.getToken(deviceId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("token",token);
        map.put("deviceId",deviceId);
        String result=OkHttpUtil.doGetHttpRequest(configPath.getAlarms(),map,null);
        JSONObject jsonObject =JSONObject.parseObject(result);
        if(jsonObject.get("code").equals("0")){
            return setResultSuccess();
        }
        return setResultError("获取所有闹钟出错，请联系后台管理员Bob");
    }




}
